package util;

import entity.Person;
import entity.PersonService;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Francis
 */
public class LoginManager {

    public static void login(HttpSession session, String loginName, Integer personID) {
        session.setAttribute("loginname", loginName);
        session.setAttribute("personID", personID);
    }

    public static boolean hasLogin(HttpSession session) {
        if (session == null || session.getAttribute("personID") == null) {
            return false;
        } else {
            return true;
        }
    }

    public static String getCurrentLoginName(HttpSession session) {
        if (session != null) {
            return (String) session.getAttribute("loginname");
        }
        return null;
    }

    public static Integer getCurrentUserID(HttpSession session) {
        if (session != null) {
            return (Integer) session.getAttribute("personID");
        }
        return null;
    }

    public static Person getCurrentLoginPerson(HttpSession session) {
        Integer id = getCurrentUserID(session);
        if (id != null) {
            return PersonService.getById(id);
        }
        return null;
    }
}
